import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '工作台', svgPath: '/dashboard' }
      }
    ]
  },
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   meta: { title: '工作台', svgPath: '/dashboard' },
  //   children: [
  //     {
  //       path: 'dashboard',
  //       name: 'Dashboard',
  //       component: () => import('@/views/dashboard/index'),
  //       meta: { title: '工作台', svgPath: '/dashboard' }
  //     }
  //   ]
  // },

  {
    path: '/element',
    component: Layout,
    redirect: '/element/index',
    name: 'Element',
    meta: { title: '素材中心', svgPath: '/element' },
    children: [
      {
        path: 'index',
        name: 'ElementIndex',
        component: () => import('@/views/element/index/index'),
        meta: { title: '素材库', svgPath: '/element/index', child: true }
      },
      {
        path: 'product',
        name: 'ElementProduct',
        component: () => import('@/views/element/product/index'),
        meta: { title: '成片库', svgPath: '/element/product', child: true }
      }
    ]
  },
  {
    path: '/creation',
    component: Layout,
    redirect: '/creation/smart',
    name: 'Creation',
    meta: { title: '创作中心', svgPath: '/creation' },
    children: [
      {
        path: 'smart',
        name: 'CreationSmart',
        component: () => import('@/views/creation/smart/index'),
        meta: { title: '智能剪辑', svgPath: '/creation/smart', child: true }
      },
      {
        path: 'hot',
        name: 'CreationHot',
        component: () => import('@/views/creation/hot/index'),
        meta: { title: '爆款案例', svgPath: '/creation/hot', child: true }
      }
    ]
  },
  {
    path: '/account',
    component: Layout,
    redirect: '/account/auth',
    name: 'Account',
    meta: { title: '账号管理', svgPath: '/account' },
    children: [
      {
        path: 'auth',
        name: 'AccountAuth',
        component: () => import('@/views/account/auth/index'),
        meta: { title: '授权账户', svgPath: '/account/auth', child: true }
      },
      {
        path: 'message',
        name: 'AccountMessage',
        component: () => import('@/views/account/message/index'),
        meta: { title: '私信管理', svgPath: '/account/message', child: true }
      },
      {
        path: 'comment',
        name: 'AccountComment',
        component: () => import('@/views/account/comment/index'),
        meta: { title: '评论管理', svgPath: '/account/comment', child: true }
      },
      {
        path: 'autoReply',
        name: 'AccountAutoReply',
        component: () => import('@/views/account/autoReply/index'),
        meta: { title: '自动回复', svgPath: '/account/autoReply', child: true }
      }
    ]
  },
  {
    path: '/market',
    component: Layout,
    redirect: '/market/video',
    name: 'Market',
    meta: { title: '矩阵营销', svgPath: '/market' },
    children: [
      {
        path: 'video',
        name: 'MarketVideo',
        component: () => import('@/views/market/video/index'),
        meta: { title: '视频发布', svgPath: '/market/video', child: true }
      },
      {
        path: 'scan',
        name: 'MarketScan',
        component: () => import('@/views/market/scan/index'),
        meta: { title: '扫码发布', svgPath: '/market/scan', child: true }
      },
      {
        path: 'article',
        name: 'MarketArticle',
        component: () => import('@/views/market/article/index'),
        meta: { title: '图文发布', svgPath: '/market/article', child: true }
      },
      {
        path: 'publish',
        name: 'MarketPublish',
        component: () => import('@/views/market/publish/index'),
        meta: { title: '发布明细', svgPath: '/market/publish', child: true }
      }
    ]
  },
  {
    path: '/business',
    component: Layout,
    redirect: '/business/keywords',
    name: 'Business',
    meta: { title: '企业营销', svgPath: '/business' },
    children: [
      {
        path: 'keywords',
        name: 'businesskeywords',
        component: () => import('@/views/business/keywords/index'),
        meta: {
          title: '关键词管理',
          svgPath: '/business/keywords',
          child: true
        }
      },
      {
        path: 'info',
        name: 'businessinfo',
        component: () => import('@/views/business/info/index'),
        meta: { title: '信息流广告', svgPath: '/business/info', child: true }
      },
      {
        path: 'search',
        name: 'businesssearch',
        component: () => import('@/views/business/search/index'),
        meta: { title: '搜索广告', svgPath: '/business/search', child: true }
      }
    ]
  },
  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/setting',
    name: 'Personal',
    meta: { title: '个人中心', svgPath: '/personal' },
    children: [
      {
        path: 'setting',
        name: 'PersonalSetting',
        component: () => import('@/views/personal/setting/index'),
        meta: { title: '账号设置', svgPath: '/personal/setting', child: true }
      },
      {
        path: 'account',
        name: 'PersonalAccount',
        component: () => import('@/views/personal/account/index'),
        meta: { title: '账户中心', svgPath: '/personal/account', child: true }
      },
      {
        path: 'staff',
        name: 'PersonalStaff',
        component: () => import('@/views/personal/staff/index'),
        meta: { title: '员工管理', svgPath: '/personal/staff', child: true }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
